package com.ilancuo.datastructuresandalgorithmsinjava.ch06_recursion;

import java.util.Scanner;

/**
 * 1, 3, 6, 10, 15, 21...
 * Created by ilancuo on 2018/8/29.
 */
public class Triangle {
    static int theNumber;

    public static void main(String[] args) {
        System.out.print("Enter a number: ");
        theNumber = new Scanner(System.in).nextInt();
        int theAnswer = triangle(theNumber);
        System.out.println("Triangle = " + theAnswer);
    }

    private static int triangle(int n) {
        System.out.println("Entering: n = " + n);
        if (n == 1) {
            System.out.println("Returning 1");
            return 1;
        } else {
            int temp = n + triangle(n - 1);
            System.out.println("Returning " + temp);
            return temp;
        }
    }
}
